#include "GLHeaders.hpp"
#include "Model.hpp"
#include "Vector.hpp"
#include "Bone.hpp"

Bone::Bone(int nbChild){
	if(nbChild <= 0){
		childNumber = 0;
		child = NULL;
	}
	else{
		childNumber = nbChild;
		child = new Bone*[childNumber];
		for(int loop=0; loop < childNumber; loop++)
			*(child+loop) = NULL;			  
	}

	translation = Vector::ZERO;
	rotation = Vector::ZERO;
	scale = Vector::ONES;
	model = NULL;
}

Bone::~Bone(){
	if(childNumber != 0)
		delete[] child;
}

//SetDrawFunc must be done!!!
void Bone::Draw(void){
	glPushMatrix();
	glTranslatef(translation.x, translation.y, translation.z);
	glRotatef(angle, rotation.x, rotation.y, rotation.z);
	glScalef(scale.x, scale.y, scale.z);
	model->Draw();
	for(int loop=0; loop < childNumber; loop++)
		(*(child+loop))->Draw();	
	glPopMatrix();
}

//Accessors
int Bone::GetChildNumber(void) const{
	return childNumber;
}

Vector Bone::GetTranslation(void) const{
	return Vector(translation);
}

Vector Bone::GetRotation(void) const{
	return Vector(rotation);
}

GLfloat Bone::GetAngle(void) const{
	return angle;
}

Vector Bone::GetScale(void) const{
	return Vector(scale);
}

//Mutators
void Bone::SetConnect(const int id, Bone* b){
	if((id < childNumber)&&(id >= 0)){
		*(child+id) = b;
	}
}

void Bone::SetTranslation(
		const GLfloat fx,
		const GLfloat fy,
		const GLfloat fz){
	translation = Vector(fx, fy, fz);
}

void Bone::SetTranslation(const Vector& vector){
	translation = vector;
}

void Bone::SetRotation(
		const GLfloat fa,
		const GLfloat fx,
		const GLfloat fy,
		const GLfloat fz){
	angle = fa;
	rotation = Vector(fx, fy, fz);
}

void Bone::SetRotation(
		const GLfloat fx,
		const GLfloat fy,
		const GLfloat fz){
	rotation = Vector(fx, fy, fz);
}

void Bone::SetRotation(const Vector& vector){
	rotation = vector;
}

void Bone::SetAngle(const GLfloat fa){
	angle = fa;
}

void Bone::SetScale(
		const GLfloat fx,
		const GLfloat fy,
		const GLfloat fz){
	scale = Vector(fx, fy, fz);
}

void Bone::SetScale(const Vector& vector){
	scale = vector;
}

void Bone::SetModel(Model* model3D){
	model = model3D;
}

